package cn.v2tila.rsosa.student.controller;

import cn.v2tila.rsosa.student.entity.DeliveryEntity;
import cn.v2tila.rsosa.student.entity.VitaeEntity;
import cn.v2tila.rsosa.student.entity.VitaeImgsEntity;
import cn.v2tila.rsosa.student.service.*;
import cn.v2tila.rsosa.student.utils.utils;
import cn.v2tila.rsosa.student.vo.CheckVitaeVO;
import cn.v2tila.rsosa.student.vo.RequestVO;
import cn.v2tila.rsosa.student.vo.ResultVO;
import cn.v2tila.rsosa.student.vo.VitaeVO;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.security.krb5.internal.crypto.RsaMd5CksumType;

import java.util.ArrayList;
import java.util.List;

/**
 * @author v2tila on 21:16 2019/3/31
 */
@RestController
@RequestMapping(value = "vitae")
public class vitaeController {
    @Autowired
    private VitaeService vitaeService;
    @Autowired
    private VitaeImgsService vitaeImgsService;
    @Autowired
    private DeliveryService deliveryService;

    @RequestMapping(value = "save")
    public ResultVO save(VitaeVO vitaeVO) {
        ResultVO resultVO = new ResultVO();
        VitaeEntity vitaeEntity = new VitaeEntity();
        vitaeEntity.setName(vitaeVO.getName());
        vitaeEntity.setAddress(vitaeVO.getAddress());
        vitaeEntity.setBirthday(vitaeVO.getBirthday());
        vitaeEntity.setPhone(vitaeVO.getPhone());
        vitaeEntity.setEmail(vitaeVO.getEmail());
        vitaeEntity.setSex(vitaeVO.getSex());
        vitaeEntity.setTitle(vitaeVO.getTitle());
        vitaeEntity.setSchoolName(vitaeVO.getSchoolName());
        vitaeEntity.setCollegeName(vitaeVO.getCollegeName());
        vitaeEntity.setClassName(vitaeVO.getClassName());
        vitaeEntity.setIntro(vitaeVO.getIntro());
        vitaeEntity.setUniqueId(vitaeVO.getUniqueId());
        vitaeEntity.setAvatarUrl(vitaeVO.getAvatarUrl());
        vitaeEntity.setCreateTime(new utils().getTime());
        if (vitaeVO.getVitaeImgs() == null) vitaeEntity.setHasImgs(0);
        else
            vitaeEntity.setHasImgs(1);
        VitaeEntity isSuccess = vitaeService.saveAndFlush(vitaeEntity);
        if (isSuccess == null) {
            resultVO.setCode(0);
            resultVO.setMsg("vitae存储出错");
        } else {
            if (isSuccess.getHasImgs() == 1) {
                Gson gson = new Gson();
                List<String> vitaeImgs = gson.fromJson(vitaeVO.getVitaeImgs(), new TypeToken<List<String>>() {
                }.getType());
                for (int i = 0; i < vitaeImgs.size(); i++) {
                    VitaeImgsEntity vitaeImgsEntity = new VitaeImgsEntity();
                    vitaeImgsEntity.setVitaeId(isSuccess.getId());
                    vitaeImgsEntity.setFileId(vitaeImgs.get(i));
                    VitaeImgsEntity viImgIsSuc = vitaeImgsService.saveAndFlush(vitaeImgsEntity);
                    if (viImgIsSuc == null) {
                        resultVO.setCode(0);
                        resultVO.setMsg("生活照存储出错");
                        return resultVO;
                    }
                }
            }
            resultVO.setCode(1);
        }
        return resultVO;
    }

    @RequestMapping(value = "getVitaeDetail")
    public ResultVO getVitaeDetail(Integer id) {
        ResultVO resultVO = new ResultVO();
        VitaeEntity vitaeEntity = vitaeService.findById(id);
        if(vitaeEntity==null){
            resultVO.setCode(0);
            resultVO.setMsg("出错，请联系管理员");
        }else{
            VitaeVO vitaeVO = new VitaeVO();
            vitaeVO.setUniqueId(vitaeEntity.getUniqueId());
            vitaeVO.setTitle(vitaeEntity.getTitle());
            vitaeVO.setName(vitaeEntity.getName());
            vitaeVO.setSex(vitaeEntity.getSex());
            vitaeVO.setBirthday(vitaeEntity.getBirthday());
            vitaeVO.setPhone(vitaeEntity.getPhone());
            vitaeVO.setEmail(vitaeEntity.getEmail());
            vitaeVO.setAddress(vitaeEntity.getAddress());
            vitaeVO.setAvatarUrl(vitaeEntity.getAvatarUrl());
            vitaeVO.setSchoolName(vitaeEntity.getSchoolName());
            vitaeVO.setCollegeName(vitaeEntity.getCollegeName());
            vitaeVO.setClassName(vitaeEntity.getClassName());
            vitaeVO.setIntro(vitaeEntity.getIntro());
            if (vitaeEntity.getHasImgs()==1){
                List<String> vitaeImgs = new ArrayList<String>();
                List<VitaeImgsEntity> vitaeImgsEntities = vitaeImgsService.findByVitaeId(id);
                Gson gson = new Gson();
                for (int i = 0; i < vitaeImgsEntities.size(); i++) {
                    String vitaeImg = vitaeImgsEntities.get(i).getFileId();
                    vitaeImgs.add(vitaeImg);
                }
                String vitaeImgsJson = gson.toJson(vitaeImgs);
                vitaeVO.setVitaeImgs(vitaeImgsJson);
            }
            resultVO.setVitaeVO(vitaeVO);
            resultVO.setCode(1);
        }
        return resultVO;

    }


    @RequestMapping(value = "getVitae")
    public ResultVO getVitae(String uniqueId){
        ResultVO resultVO = new ResultVO();
        List<VitaeEntity> vitaeEntityList = vitaeService.findByUniqueId(uniqueId);
        resultVO.setVitaeList(vitaeEntityList);
        return resultVO;
    }

    @RequestMapping(value = "deleteVitae")
    public ResultVO deleteVitae(Integer id){
        ResultVO resultVO = new ResultVO();
        try {
            vitaeService.deleteById(id);
        }catch (Exception e){
            resultVO.setCode(0);
            resultVO.setMsg("无法删除 请联系管理员");
        }
        resultVO.setCode(1);
        return resultVO;
    }

    @RequestMapping(value = "checkVitae")
    public ResultVO checkVitae(Integer assoId,Integer code,int page) {
        ResultVO resultVO = new ResultVO();
        List<CheckVitaeVO> checkVitaeVOList = new ArrayList<CheckVitaeVO>();

        Page<DeliveryEntity> pages = deliveryService.findByAssoIdAndCode(assoId, code,page);
        List<DeliveryEntity> deliveryEntityList = pages.getContent();
        resultVO.setCount(pages.getTotalPages());
        resultVO.setTotal(pages.getTotalElements());

        for (int i = 0; i < deliveryEntityList.size(); i++) {
            CheckVitaeVO checkVitaeVO = new CheckVitaeVO();
            int vitaeId = deliveryEntityList.get(i).getVitaeId();
            int deliveryId = deliveryEntityList.get(i).getId();
            VitaeEntity vitaeEntity = vitaeService.findById(vitaeId);
            String avatarUrl = vitaeEntity.getAvatarUrl();
            String name = vitaeEntity.getName();
            String depName = deliveryEntityList.get(i).getDepName();
            String deliveryTime = deliveryEntityList.get(i).getCreateTime();
            checkVitaeVO.setAvatarUrl(avatarUrl);
            checkVitaeVO.setName(name);
            checkVitaeVO.setDepName(depName);
            checkVitaeVO.setVitaeId(vitaeId);
            checkVitaeVO.setDeliveryTime(deliveryTime);
            checkVitaeVO.setDeliveryId(deliveryId);
            checkVitaeVOList.add(checkVitaeVO);

        }
        resultVO.setCheckVitaeVOList(checkVitaeVOList);
        return resultVO;
    }

    @RequestMapping(value = "changeDeliveryCode")
    public void changeDeliveryCode(int id,int code){

        DeliveryEntity deliveryEntity = deliveryService.findById(id);
        if(deliveryEntity.getCode()==2&&code==1){

        }else{
            deliveryEntity.setCode(code);
            deliveryService.saveAndFlush(deliveryEntity);
        }
    }
}
